const app = getApp()
const api = require('../../../../utils/api')

Page({
  data: {
    orderNo: '',
    order: null,
    loading: false,
    // 商家订单状态映射表
    statusMap: {
      1: '新订单',
      2: '处理中',
      3: '已发货',
      4: '已完成',
      5: '已取消'
    }
  },

  onLoad(options) {
    if (options.orderNo) {
      this.setData({
        orderNo: options.orderNo
      });
      this.loadOrderDetail();
    }
  },

  // 加载订单详情
  async loadOrderDetail() {
    if (this.data.loading) return;

    try {
      this.setData({ loading: true });
      const res = await api.getMerchantOrder(this.data.orderNo);
      
      if (res.success) {
        const order = {
          ...res.data,
          statusText: this.data.statusMap[res.data.status],
          products: res.data.orderItems.map(item => ({
            id: item.productId,
            name: item.productName,
            price: item.productPrice,
            quantity: item.quantity,
            imageUrl: item.product?.imageUrl || '/images/default-product.png',
            processingStatus: item.processingStatus,
            remark: item.remark
          }))
        };
        
        this.setData({ order });
      } else {
        wx.showToast({
          title: res.message || '加载失败',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('加载订单详情失败:', error);
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 格式化价格
  formatPrice(price) {
    return parseFloat(price).toFixed(2)
  },

  // 接单
  async onAcceptOrder() {
    await this.updateOrderStatus(2, '商家已接单');
  },

  // 发货
  async onShipOrder() {
    await this.updateOrderStatus(3, '商家已发货');
  },

  // 完成订单
  async onCompleteOrder() {
    await this.updateOrderStatus(4, '订单已完成');
  },

  // 取消订单
  async onCancelOrder() {
    await this.updateOrderStatus(5, '商家已取消订单');
  },

  // 更新订单状态
  async updateOrderStatus(status, notes) {
    try {
      const res = await api.updateMerchantOrderStatus(this.data.orderNo, status, notes);
      if (res.success) {
        wx.showToast({
          title: '操作成功',
          icon: 'success'
        });
        // 重新加载订单详情
        this.loadOrderDetail();
      } else {
        wx.showToast({
          title: res.message || '操作失败',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('更新订单状态失败:', error);
      wx.showToast({
        title: '操作失败',
        icon: 'none'
      });
    }
  },

  // 拨打电话
  onCallCustomer() {
    if (this.data.order && this.data.order.receiverPhone) {
      wx.makePhoneCall({
        phoneNumber: this.data.order.receiverPhone
      });
    }
  },

  // 复制订单号
  onCopyOrderNo() {
    if (this.data.order && this.data.order.orderNo) {
      wx.setClipboardData({
        data: this.data.order.orderNo,
        success: () => {
          wx.showToast({
            title: '已复制订单号',
            icon: 'success'
          });
        }
      });
    }
  },

  // 复制收货地址
  onCopyAddress() {
    if (this.data.order && this.data.order.receiverAddress) {
      wx.setClipboardData({
        data: this.data.order.receiverAddress,
        success: () => {
          wx.showToast({
            title: '已复制地址',
            icon: 'success'
          });
        }
      });
    }
  }
}) 